--#include "data\config\boss\HuntBossTreasureConfig.lua" once

--[[
注册一些事件
]]
function ResisteHuntBossTreasureProc()
	--print("ResisteHuntBossTreasureProc...")
	for huntIdx, huntCfg in ipairs( HuntBossTreasureCfg ) do
		SceneExitDispatcher.register( huntCfg.sceneId, OnCommonSceneExit )					--注册离开场景、副本后触发的事件
	end
end

--[[
进入打宝地图
]]
function EnterHuntBossTreasure( sysarg, huntIdx )
	--print("EnterHuntBossTreasure, huntIdx="..huntIdx)
	local huntCfg = HuntBossTreasureCfg[huntIdx]
	if not huntCfg then
		return
	end

	if CommonEnterScence( sysarg, huntCfg, GameLog.clEnterHuntBossTreasureFee, Lang.ScriptTips.HuntBossTreasureLog01 ) then
		local dDvar = Actor.getDyanmicVar(sysarg)
		dDvar.enterHuntBossTreasureTime = System.getCurrMiniTime()

		--注册回城的延迟消息
		Actor.removeEntityScriptDelayMsg( sysarg, 17 )
		--[[最后的true表示实体死亡，消息保留]]
		Actor.postEntityScriptDelayMsg( sysarg, huntCfg.beInTime*1000, 0, 17,0,0,0,0, false, true )

		--[[有时可能活动面板没有打开成功，几秒后再打开一次
		Actor.removeEntityScriptDelayMsg( sysarg, DELAY_MSG_ACTIVITY_PANEL )
		Actor.postEntityScriptDelayMsg( sysarg, 5*1000, 0, DELAY_MSG_ACTIVITY_PANEL,0,0,0,0,false,true )
		System.broadcastTipmsgLimitLev( string.format(Lang.ScriptTips.BossHome005, Actor.getName( sysarg )), ttChatWindow, bossHomeCfg.enterLevelLimit[2])
		]]
		InitHuntBossTreasure(sysarg, huntCfg)

		Actor.closeDialogs( sysarg, enLinkBossWnd )	
	end
end

function InitHuntBossTreasure(sysarg, huntCfg)
	local restTime = huntCfg.beInTime
	InitOnlyButtonPanel( sysarg, 2, enRightPanelActivity_HuntBossTreas, restTime, Lang.ScriptTips.HuntBossTreasureName )
	local activityId = enCountDownPanelActivity_HuntBossTreasure
	local panelInfo = {}

	panelInfo.title 	= Lang.ScriptTips.HuntBossTreasureName			
	--panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	--panelInfo.restTime 	= restTime
	panelInfo.content	= {}

	for bossIdx, bossId in ipairs( huntCfg.boss ) do
		local bossCfg = MobBossCommonConfig[bossId]
		if bossCfg then
			local desc 	= System.getMonsterNameById( bossCfg.monsterId )
			local lastTime 	= 0
			local bossNum = System.getMonsterCountById( bossCfg.monsterId, bossCfg.sceneId, 0 )
			if bossNum > 0 then		--BOSS存在
				lastTime = 0
			else
				local timeCd = 0
				local monCfg = MobBossCommonConfig[bossCfg.monsterId]
				if monCfg then
					timeCd = GetBossRevivalCd(bossId, monCfg.revivalCd )
				end
				lastTime = GetCommonBossLastTime( bossCfg.monsterId, timeCd ) --GetMayaPalaceBossLastTime( palaceCfg.palaceIdx, bossCfg )
			end
			panelInfo.content[bossIdx] = { desc, lastTime, bossCfg.monsterId }
		end
	end
	OpenCountDownPanel( sysarg, activityId, panelInfo )
end

function ChangeHuntBossCountDownPanel( sysarg, huntCfg, bossId )
	local content = {}
	local bossCfg 	= MobBossCommonConfig[bossId]
	if bossCfg then
		local desc 		= System.getMonsterNameById( bossCfg.monsterId )
		local lastTime 	= 0
		local bossNum 	= System.getMonsterCountById( bossCfg.monsterId, bossCfg.sceneId, 0 )
		if bossNum > 0 then		--BOSS存在
			lastTime = 0
		else
			local timeCd = 0
			local monCfg = MobBossCommonConfig[bossCfg.monsterId]
			if monCfg then
				timeCd = GetBossRevivalCd(bossId, monCfg.revivalCd )
			end
			lastTime = GetCommonBossLastTime( bossCfg.monsterId, timeCd ) --GetMayaPalaceBossLastTime( palaceCfg.palaceIdx, bossCfg )
		end

		local bossIdx = GetBossIdxByBossId( huntCfg, bossCfg.monsterId )
		if bossIdx > 0 then
			content.idx 	= bossIdx
			content.info	= { desc, lastTime, bossCfg.monsterId }

			ChangeCountDownPanel( sysarg, content )
		end
	end
end
function BroadHuntBossTreasurePanelChange( huntCfg, bossId )
	local bossNum = System.getMonsterCountById( bossId, huntCfg.sceneId, 0 )
	--print("BroadMayaPalaceBossChange, bossNum="..bossNum)
	local playerList = LuaHelp.getSceneActorListById( huntCfg.sceneId ) or {}
	for i,player in ipairs( playerList ) do
		ChangeHuntBossCountDownPanel( player, huntCfg, bossId )
	end
end
function OnHuntBossTreasureBossMob(monster, monId, sceneId, x, y)
	local huntCfg = GetHuntBossTreasureCfgBySceneId( sceneId )
	if not huntCfg then
		return
	end

	SetCommonBossKilledTime( monId, 0 )
	BroadHuntBossTreasurePanelChange( huntCfg, monId )
end

function OnHuntBossTreasureBossDeath(monster, monId)
	local sceneId = Actor.getSceneId( monster )
	local huntCfg = GetHuntBossTreasureCfgBySceneId( sceneId )
	if not huntCfg then
		return
	end

	local now = System.getCurrMiniTime()
	SetCommonBossKilledTime( monId, now )
	BroadHuntBossTreasurePanelChange( huntCfg, monId )
end

function GetHuntBossTreasureCfgBySceneId( sceneId )
	for i, v in ipairs( HuntBossTreasureCfg ) do
		if v.sceneId == sceneId then
			return v
		end
	end
	return nil
end

ResisteHuntBossTreasureProc()
